<?php
class CscModel extends CActiveRecord {
	/** 添加表单信息 **/
	public function add_($data=array()){
		$data = $data ? $data: $this->params_();
		$model = array2object($this->create(), $data);
		try {
			$id = $model->save ();
			$insertId = 0;
			if($id) {
				$insertId = $model->getPrimaryKey();
			}
			return $insertId;
		}catch (Exception $e){
			Tools::rlog($e,$e->getMessage());
		}
		return 0;
	}
	//更新
	public function update_($data =array(),$where,$db="db",$table_name=null){

		$v = null;
		if(null==$where || ""==$where) return "";
		foreach($data as $key=>$val){
			$v .= $v ? ",`{$key}` = '{$val}'":"`{$key}`='{$val}'";
		}
		$table_name = $table_name?$table_name:$this->tableName();
		$sql="UPDATE {$table_name} SET {$v} WHERE ".$where;
		return $this->getDbConnection()->createCommand($sql)->execute();
	}

	//针对跨数据库操作
	static function insertSql($data=array(),$tablename,$db = "db"){
		$shama = "";
		$val = "";
		foreach($data as $k=>$v){
			$shama .= $shama?",`{$k}`" : "`$k`";
			$val .= $val? ",'{$v}'":" '{$v}'";
		}
		$sql="INSERT INTO {$tablename} ({$shama}) VALUES({$val})";
		try{
			self::create()->getDbConnection()->createCommand($sql)->execute();
			return 1;
		}catch (Exception $e){
			return 0;
		}
	}

	static $_instance =null;
	public static function  create(){
		return new self;
	}

	public function get_total($table=null,$where,$db = "db"){
		$table = $table ?$table :$this->tableName();
		$sql = "SELECT COUNT(1) AS mk FROM {$table} WHERE {$where}";
		return $this->countBySql($sql);
	}

	public function fetchOne($where,$idx = '*',$order='',$db="db"){
        $sql="SELECT {$idx} FROM {$this->tableName()} WHERE  {$where}  LIMIT 1 ";
	    if($order){
            $sql="SELECT {$idx} FROM {$this->tableName()} WHERE  {$where} ORDER BY {$order} LIMIT 1 ";
        }else{
            $sql="SELECT {$idx} FROM {$this->tableName()} WHERE  {$where}  LIMIT 1 ";
        }

		$row = $this->getDbConnection()->createCommand($sql)->queryAll();
		return isset($row[0])?$row[0]:array();
	}

	public function fetchAll($where,$page=0,$limit = 15,$order = null,$idx = "*",$db="db"){
		$sql = "SELECT {$idx} FROM {$this->tableName()}";
		if($where){
			$sql .=" WHERE {$where} " ;
		}
		if($order){
			$sql .= " ORDER BY {$order} ";
		}
		$offset = $limit*$page;
		$sql .=" LIMIT 	{$offset},{$limit} ";
		return $this->getDbConnection()->createCommand($sql)->queryAll();
	}

	public function delete_($where){
		$sql = "DELETE FROM {$this->tableName()} WHERE {$where} ";
		try{
			return $this->getDbConnection()->createCommand($sql)->execute();
		}catch (Exception $e){
			Tools::rlog($e,"删除失败,请重试");
		}
	}
	//一次性多条插入
	public function insertMore($data,$tablename=""){
		if(count($data)>0){
			$shama = "";

			foreach($data[0] as $k=>$v){
				$shama .= $shama?",`{$k}`" : "`$k`";
			}
			$sql="INSERT INTO {$tablename} ({$shama}) VALUES";
			$str = "";
			foreach($data as $item){
				$val = "";
				foreach($item as $v){
					$val .= $val? ",'{$v}'":" '{$v}'";
				}
				$str .= $str ?",({$val})":"({$val})";
			}
			$sql .= $str;
//			file_put_contents("d:/sql.txt",$sql);
//			die;
			try {
				$this->getDbConnection()->createCommand($sql)->execute();
			}catch(Exception $e){

			}
		}
	}
}